#!/bin/bash
# $1 = audio file to transmit
# Make sure another application (i,e: QSSTV) hasn't grabbed the
# audio output device. (Letting them have the input channel is fine)
# Assumes an ALSA audio environment-- used "aplay -L" to figure out
# the radio's audio device. Might be able to use with pulseaudio with
# the right device name?
# -- 
# Chris Baird,, <vk2cjb@gmail.com>

# for a basic RTS-controlled PTT rig
userig="0"
a=`pgrep rigctld`X
if [ "$a" == "X" ]
then
    echo "Starting rigctld"
    rigctld -T localhost -m 1 -r /dev/ttyUSB0 -p /dev/ttyUSB1 -P RTS &
    sleep 1
    userig="1"
fi

RIG=2
RADDEV=localhost:4532

#FREQ=145100000  # VK's WIA bandplan FM Simplex Datamodes freq
#MODE=PKTFM
#MODE=PKTUSB
#KEYMODE=1
#
#rigctl -m $RIG -r $RADDEV -s 38400 F $FREQ M $MODE 0
#a=`rigctl -m $RIG -r $RADDEV -s 38400 f`
#if [ "X"$a != "X"$FREQ ]; then echo "Couldn't set radio"; exit; fi

echo -n "Keying transmitter (1 sec)"
rigctl -m $RIG -r $RADDEV T 1
sleep 1
echo

echo -n "Transmitting audio"
mplayer -msglevel all=-1:statusline=5 -ao alsa:device=hw=CODEC $1 2>/dev/null
echo

echo -n "Unkeying transmitter"
sleep 1
rigctl -m $RIG -r $RADDEV T 0
echo

if [ userig == "1" ]
then
    echo "Killing rigctld"
    killall -9 rigctld
    sleep 0.5
    killall -9 rigctld
fi

exit
